#!/usr/bin/ruby
#

require 'mysql'
require '../db_setting'

db = Mysql::new(DB_HOST, DB_USER, DB_PASS, "test")#DB_NAME)

buf_link = ''
buf_node = ''


$stderr.print "load data..."

res = db.query("select * from browse_nodes")
res.each_hash do |row|
  id = row['id'].to_i
  name = row['name']

  buf_node << '%d [label="%s"];%s' % [id, name, "\n"]
end

res = db.query("select * from link_browse_node2browse_node")
res.each_hash do |row|
  parent_id = row['parent_id'].to_i
  child_id = row['child_id'].to_i

  buf_link << "#{parent_id} -> #{child_id};\n"
end



$stderr.puts "ok"


print <<EOS
digraph G {
  graph [rankdir=\"LR\"];
  #{buf_link}
  #{buf_node}
}
EOS

db.close
